/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditCarPanel.java
 *
 * Created on Jan 4, 2011, 9:20:05 PM
 */

package Graphic.DataManager.CarPackage;

import Client.RequestI;
import Graphic.MainForm;
import XMLPack.Save2Csv;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Cristina
 */
public class EditCarPanel extends javax.swing.JPanel {

    /** Creates new form EditCarPanel */
    private MainForm form = null;
    RequestI request = null;
    public EditCarPanel(RequestI r, MainForm f) {
        form = f;
        request = r;
        initComponents();
        
        model = (DefaultTableModel) table.getModel();
        table.setModel(model);
        model.setNumRows(0);
        //model.addRow(new Object[]{"zozo", "dodod", "coco", "lolo"});

    }

    public void Show()
    {
        while(model.getRowCount()>0)
         model.removeRow(0);

        String name = request.selectAll("dmstc", "car");
        request.getClient().SetRequest(name);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        deleteCarBtn = new javax.swing.JButton();
        editCarBtn = new javax.swing.JButton();
        saveBtn = new javax.swing.JButton();

        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Number", "Model", "SeatsNumber", "kmNumber", "maintenanceCost", "fuelConsumption"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(table);
        table.getColumnModel().getColumn(0).setResizable(false);
        table.getColumnModel().getColumn(1).setResizable(false);
        table.getColumnModel().getColumn(2).setResizable(false);
        table.getColumnModel().getColumn(3).setResizable(false);
        table.getColumnModel().getColumn(4).setResizable(false);
        table.getColumnModel().getColumn(5).setResizable(false);

        jScrollPane2.setViewportView(jScrollPane1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Edit car");

        deleteCarBtn.setText("Delete");
        deleteCarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCarBtnActionPerformed(evt);
            }
        });

        editCarBtn.setText("Edit");
        editCarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCarBtnActionPerformed(evt);
            }
        });

        saveBtn.setText("Save");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 484, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(deleteCarBtn)
                        .addGap(27, 27, 27)
                        .addComponent(editCarBtn)
                        .addGap(44, 44, 44)
                        .addComponent(saveBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteCarBtn)
                    .addComponent(editCarBtn)
                    .addComponent(saveBtn))
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void deleteCarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCarBtnActionPerformed
       if(model.getRowCount() >0 && table.getSelectedRow() != -1){
         String carNumber = (String)model.getValueAt(table.getSelectedRow(), 0);
         String name = request.deleteCar("dmstc", carNumber);
         request.getClient().SetRequest(name);
         Show();
        }
    }//GEN-LAST:event_deleteCarBtnActionPerformed

    private void editCarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCarBtnActionPerformed

       if(model.getRowCount() >0 && table.getSelectedRow() != -1){
         String carNumber = (String)model.getValueAt(table.getSelectedRow(), 0);
         form.modifyCar(carNumber);
       }
    }//GEN-LAST:event_editCarBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        String wd = System.getProperty("user.dir");
       JFileChooser fc = new JFileChooser(wd);
    int rc = fc.showDialog(null, "Select Data File");
    if (rc == JFileChooser.APPROVE_OPTION)
    {
    File file = fc.getSelectedFile();
    String filename = file.getAbsolutePath();
    Save2Csv saveObj = new Save2Csv();
    saveObj.Save(table, filename);
    }
    else
    System.out.println("File chooser cancel button clicked");
    
    }//GEN-LAST:event_saveBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteCarBtn;
    private javax.swing.JButton editCarBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton saveBtn;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel model;

    public DefaultTableModel getTableModel() {return model;}
}
